package com.sky.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.sky.domain.User;
import com.sky.exception.BookException;
import com.sky.exception.UserException;
import com.sky.service.UserService;
import com.sky.utils.UploadUtils;

public class UpdateUserinfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//数据提取
		Map<String,String> map = UploadUtils.HandleFormByUpload(request, getServletContext());
		User user=new User();
		try {
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("=========修改信息=========");
		System.out.println(user.toString());
		//逻辑处理
		UserService userservice=new UserService();
		//分发转向
		try {
			userservice.updateuserinfo(user);
			request.getRequestDispatcher("queryUserListServlet").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("error02_msg", e.getMessage());
			request.getRequestDispatcher("userInfoServlet?id="+user.getId()).forward(request, response);
		} catch (UserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("error02_msg", e.getMessage());
			request.getRequestDispatcher("queryUserListServlet").forward(request, response);
		}
	}

}
